package com.huawei.chengdu.meetingdemo.view.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.huawei.chengdu.meetingdemo.R;

public class RequestFloatPermissionDialog extends Dialog {
    private static final String TAG = RequestFloatPermissionDialog.class.getSimpleName();
    private LinearLayout sureReq;
    private LinearLayout cancelReq;

    public RequestFloatPermissionDialog(@NonNull Context context) {
        super(context);
        setContentView(R.layout.req_float_permission_dialog);
        sureReq = findViewById(R.id.sure_float_permission);
        cancelReq = findViewById(R.id.cancel_float_permission);
        initListener(context);
    }

    @Override
    public void show() {
        setCanceledOnTouchOutside(false);
        super.show();
    }

    private void initListener(Context context) {
        sureReq.setOnClickListener((v -> {
            reqFloatPermissions(context);
            dismiss();
        }));
        cancelReq.setOnClickListener((v -> {
            Toast.makeText(context, "未开启悬浮窗权限", Toast.LENGTH_SHORT).show();
            dismiss();
        }));
    }

    private void reqFloatPermissions(Context context) {
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.fromParts("package", context.getPackageName(), null));
        context.startActivity(intent);
    }
}
